<?php
class studentController extends Zend_Controller_Action{
    
      public function init()
    {
    
       	Zend_Loader::loadClass('StudentsModel');
        Zend_Loader::loadClass('InfoClassModel');
       	Zend_Loader::loadClass('SubjectsModel');
       	Zend_Loader::loadClass('ExercisesModel');
        Zend_Loader::loadClass('sExerciseModel');
        Zend_Loader::loadClass('PointModel');
    }
    
    public function preDispatch(){
    	
    	/*
    	 * Load template
    	*/
    	$layoutPath = APPLICATION_PATH  . '/templates/default/default';
    	$option = array (	'layout' 		=> 'index',
    			'layoutPath' 	=> $layoutPath );
    	Zend_Layout::startMvc ( $option );
    	
    	$this->view->module = $this->_request->getModuleName();
    	$this->view->controller =  $this->_request->getControllerName();
    	$this->view->action =  $this->_request->getActionName();
    }

    public function indexAction(){
        $lgNamespace = new Zend_Session_Namespace('enrol_teacher');
        $auth = Zend_Auth::getInstance();
        
        if(($lgNamespace->enrol_teacher !=1)&& ($auth->hasIdentity())){
             
             
             $info = $auth->getIdentity();
             $id_sv= $info->id;
             $model= new InfoClassModel();
             $data= $model->getClassDbbyIdsv($id_sv);
             //Zend_Debug::dump($data);die;
             $id_sub= $data['id_subjects'];
             $list= SubjectsModel::getDbRelativeById($id_sub);
             $this->view->list = $list;
             
         
        }
        
    }
    
    public function showExerciseAction(){
            if($_GET['s_id']!=''){ 
            $s_id= $_GET['s_id'];
            $list= ExercisesModel::getDbBySubjectid($s_id);
            $model= new SubjectsModel();
            $data= $model->getDbbyID($s_id);
            $this->view->data= $data;
            //Zend_Debug::dump($data);Zend_Debug::dump($list);die;
            $this->view->list= $list;
            }
        
    }
    
     public function editProfileAction(){
        $stu_id= $_GET['id'];
        $model= new StudentsModel();
        $list= $model->getDbbyID($stu_id);
        $this->view->data= $list;
        
        if($this->_request->isPost()){
            $stu_id= $this->_request->getParam('id');
            $old_pass= $this->_request->getParam('old_pass');
            $old_pass2= base64_encode($this->_request->getParam('old_pass2'));
            $new_pass= $this->_request->getParam('new_pass');
            $retype_pass= $this->_request->getParam('retype_pass');
            $name= $this->_request->getParam('name');
            $shsv= $this->_request->getParam('shsv');
            $birthday= $this->_request->getParam('birthday');
            $class= $this->_request->getParam('class');
            $course= $this->_request->getParam('course');
            $username= $this->_request->getParam('username');
            //echo $old_pass.'|'.$old_pass2.'|'.$new_pass.'|'.$retype_pass;
            if($old_pass==$old_pass2 && $new_pass==$retype_pass){
                $password=$new_pass;
                if($new_pass=='') $password=base64_decode($old_pass);
            }else{
                $password=base64_decode($old_pass);
                
            }
       	    
            if($name!='' && $course!=''&&$class!='' && $shsv!='' && $birthday!='' && $username!='' && $password!='' ){
            $data= array('name'=>$name,
    				'shsv'=>$shsv,
                    'birthday'=>$birthday,
    				'class'=>$class,
                    'course'=>$course,
    				'username'=>$username,
    				'password'=>base64_encode($password),
    		);
            
            //Zend_Debug::dump($data);die;
            
           	
    			$model->update($data,"id=".$stu_id); ?>
    		  <script type="text/javascript">
              
						 var comfirmBox;
						 comfirmBox = confirm(" Thay đổi thành công");
						 if(comfirmBox == true)
						  {
							window.location='../index';
						  }
					       
					    
    		</script>
            <?php
            
            	//$this->view->success = 1;
    	
    		}else{
    			$this->view->warning = 1;
    		}
        }
        	
    }
    
    
     public function submitExerciseAction(){
 	    
        $id_ex= $_GET['ex_id'];
        $id_stu= $_GET['stu_id'];
         // lay thong tin ve bai tap qua id  bai tap
        $list= ExercisesModel::getExById($id_ex);
        $this->view->list= $list;
        
        // kiem tra xem da nop bai tap chua neu da nop thi show ra
        $model= new sExerciseModel();
        $data=  $model->getDb($id_ex,$id_stu);
        $this->view->data= $data;
        
        // kiem tra xem co diem bai tap chua chua
        $model0= new PointModel();
        $data0= $model0->getDb($id_stu,$id_ex,'',''); 
        $this->view->data0= $data0;
        //Zend_Debug::dump($data0);die;
         
         if($this->_request->isPost()){
            /*
             * Nhan du lieu dau vao
             */
            $id= $this->_request->getParam('id'); 
           	$date = date("Y-m-d H:i:s");
            $notice["title"] = $this->_request->getParam('title');
            $notice["content"] = $this->_request->getParam('content');
            if($data[0]['link']!='') $notice["link"] = $data[0]['link'] ;else  $notice["link"] = '';
            $notice['id_exercises']= $id_ex;
            $notice['id_students']= $id_stu;
            $notice['time']= $date;
            
            
            if($notice["title"] && $notice["content"]){
                /*
                 * Xu ly upload file.
                 */
                $error_upload = 0;
                if(($_FILES["file"]["name"])){
                    $notice["link"] = ExercisesModel::uploadFile();
                    if (!preg_match('/\.(rar|zip)$/i',$notice["link"])) {
                        $error_upload = 1;
                    }
                }
                if(!$error_upload){
                    //Zend_Debug::dump($notice);die;
                   $model= new sExerciseModel();
                   
                   if($id!=''){
                         $model->update($notice,'id='.$id);
                   }else{
            
                     $model->insert($notice);
                   }
                    
                ?>
                   <script type="text/javascript">
              
						 var comfirmBox;
						 comfirmBox = confirm(" Thay đổi thành công");
						 if(comfirmBox == true)
						  {
							window.location='../student';
						  }
					       
					    
    	           	</script>
                    
                <?php   
                   
                   // sExerciseModel::insert($notice);
                   // $this->view->success = 1;
                   // $this->view->error_upload = $error_upload;
                }
                else{
                    $this->view->warning = 1;
                }
            //  Zend_Debug::dump($notice["link"]);die;
            }
            else{
                $this->view->warning = 1;
            }
            $this->view->notice = $notice;
        }
        
    }
    
    public function scoreSubjectAction(){
            $sub_id= $_GET['sub_id'];
            $stu_id= $_GET['stu_id'];
            $model= new PointModel();
            //Lấy điểm giữa kì kết hợp join lấy thông tin sinh viên
            $list= $model->getDbByIdSub($sub_id,1);
            $this->view->list= $list;
            $model2= new SubjectsModel();
            $this->view->data_sub= $model2->getDbbyID($sub_id);
            //Zend_Debug::dump($data);die;
    }
    
    public function scoreExerciseAction(){
           $id_sv= $_GET['stu_id'];
           $data= PointModel::getDbByIdStudent($id_sv);
           $this->view->list=$data;
           //Zend_Debug::dump($data);
    }
    
}